<template>
  <div  style="width:300px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="时间" required>
        <el-form-item prop="time">
          <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.time" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form-item>


      <el-form-item label="类别" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择类别">
          <el-option v-for="(item,index) in ruleForm.arr" :key="index" :label="item.name"
                     :value="item.tid"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="方式" prop="way">
        <el-select v-model="ruleForm.way" placeholder="请选择方式">
          <el-option v-for="(item,index) in ruleForm.arr2" :key="index" :label="item.name"
                     :value="item.wid"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="金额" prop="money">
        <el-input v-model="ruleForm.money"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>


    </el-form>
  </div>
</template>

<script>
  export default {
    name: "updateExpend",
    data() {
      return {
        ruleForm: {
          time: '',
          arr: '',
          arr2: '',
          money: '',
          type: '',
          way: '',
          userid: '',
          eid: '',
        },
        rules: {
          type: [
            {required: true, message: '请选择类别', trigger: 'change'}
          ],
          way: [
            {required: true, message: '请选择方式', trigger: 'change'}
          ],
          time: [
            {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
          ],
          money: [
            {required: true, pattern: /^[+-]?[0-9]+(\.[0-9]{1,2})?$/, message: '数据应为整数或小数，保留2位小数', trigger: 'blur'}
          ]

        }
      };
    },methods: {
      submitForm(formName) {
        let _this=this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm);


            let para = {
              userId: this.ruleForm.userid,
              eid: this.ruleForm.eid,
              time: this.ruleForm.time,
              expendMoney: this.ruleForm.money,
              typeId: this.ruleForm.type,
              wayId: this.ruleForm.way,
            };
            console.log(para);
            _this.$axios.post("upd",para).then(resp => {
              if(resp.data === 'success'){
                _this.$alert('修改成功！', '消息', {
                  confirmButtonText: '确定',
                  callback: action => {
                    _this.$router.push('/expend')
                  }
                })
              }
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() {
      let _this = this;
      this.$axios.get("/type/"+ this.$store.state.user.username).then(resp => {
        _this.ruleForm.arr = resp.data;
      });
      this.$axios.get("/way/"+ this.$store.state.user.username).then(resp => {
        _this.ruleForm.arr2 = resp.data;
      });
      this.$axios.get("/user/"+this.$store.state.user.username).then(resp => {
        _this.ruleForm.userid = resp.data;
      });


      let edata = this.$route.query.data;
      console.log("edata的数据");
      console.log(edata);
      this.ruleForm.eid = edata.id;
      this.ruleForm.money = edata.money;
      this.ruleForm.type = edata.tid;
      this.ruleForm.way = edata.wid;
      this.ruleForm.time = new Date(edata.time);
    }

  }
</script>

<style scoped>

</style>
